package net.oschina.simpleplus.erpc.core;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;

import net.oschina.simpleplus.erpc.proto.ERpcRequestMessage.ERpcResult;
import net.oschina.simpleplus.erpc.proto.ERpcRequestMessage.ERpcResult.Builder;
import net.oschina.simpleplus.erpc.support.Protobuf;

public class ERpcResultEntity implements Protobuf<ERpcResult>{

	private String msgId;
	private int code;
	private String msg;
	private byte[] body;
	public String getMsgId() {
		return msgId;
	}
	public void setMsgId(String msgId) {
		this.msgId = msgId;
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public byte[] getBody() {
		return body;
	}
	public void setBody(byte[] body) {
		this.body = body;
	}
	@Override
	public ERpcResult build() {
		Builder builder = ERpcResult.newBuilder().setMsgId(msgId).setCode(code);
		if(msg != null){
			builder.setMsg(msg);
		}
		
		if(body != null){
			builder.setBody(ByteString.copyFrom(body));
		}
		return builder.build();
	}
	@Override
	public byte[] toByteArray() {
		return build().toByteArray();
	}
	@Override
	public void parseFrom(byte[] bytes) throws InvalidProtocolBufferException {
		copyFrom(ERpcResult.parseFrom(bytes));
	}
	@Override
	public void copyFrom(ERpcResult t) {
		msgId = t.getMsgId();
		code = t.getCode();
		msg = t.getMsg();
		body = t.getBody().toByteArray();
	}
	
}
